package com.fit.pages.transportOffer;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.transport.TransportDAO;
import com.fit.dao.transportOffer.TransportOfferDAO;
import com.fit.entities.Transport;
import com.fit.entities.TransportOffer;
import com.fit.model.glov.GenericSelectModel;
import com.fit.pages.ErrorPage;

@AdminAccess
@UserAccess
public class AddTransportOffer {

	@Property
	private TransportOffer transportOffer;

	@InjectPage
	private ErrorPage errorPage;

	@Inject
	private TransportOfferDAO transportOfferDAO;

	@Inject
	private PropertyAccess access;

	@Property
	private Transport transport;

	@Inject
	private TransportDAO transportDAO;

	private GenericSelectModel<Transport> transports;

	public GenericSelectModel<Transport> getTransports() {
		transports = new GenericSelectModel<Transport>(transportDAO.findAll(),
				Transport.class, "name", "id", access);
		return transports;
	}

	Object onSuccess() {
		errorPage.setMessage("");

		transportOffer.setTransport(transport);
		if (transportOfferDAO.save(transportOffer)) {
			errorPage.setMessage("Transport Offer successfully added!");
			return errorPage;
		}
		errorPage.setMessage("Transport Offer is null!");
		return errorPage;

	}

}
